<?php 
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_IndexController extends Zend_Controller_Action
{

  public function init(){
		$registry = Zend_Registry::getInstance();
		$this->_db = $registry->get('db');
	}
    public function indexAction()
    {
        $this->_helper->layout->setLayout('admin');
        # If we're already logged in, just redirect
        if(Zend_Auth::getInstance()->hasIdentity())
        {
            $this->_redirect('/admin/index/adminview');
        }
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $this->_flashMessenger->getMessages();  

        $request = $this->getRequest();
        $loginForm = $this->getLoginForm();

        $errorMessage = "";

        if($request->isPost())
        {
            if($loginForm->isValid($request->getPost()))
            {

                $authAdapter = $this->getAuthAdapter();

                # get the nickname and password from the form
                $nickname = $loginForm->getValue('nickname');
                $password = $loginForm->getValue('password');
				$password = md5($password);

                # pass to the adapter the submitted nickname and password
                $authAdapter->setIdentity($nickname)
                            ->setCredential($password);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);

                # is the user a valid one?
                if($result->isValid())
                {
                    # all info about this user from the login table
                    # ommit only the password, we don't need that
                    $userInfo = $authAdapter->getResultRowObject(null, 'password');

                    # the default storage is a session with namespace Zend_Auth
                    $authStorage = $auth->getStorage();
                    $authStorage->write($userInfo);
                    
                    

                    $this->_redirect('/admin/index');
                }
                else
                {
                    $errorMessage = "Wrong nickname or password provided. Please try again.";
                }
            }
        }
        $this->view->messages[] = $errorMessage;
        $this->view->loginForm = $loginForm;
    }

    public function logoutAction()
    {
        # clear everything - session is cleared also!
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('admin/index');
    }

    /**
     * Gets the adapter for authentication against a database table
     *
     * @return object
     */
    protected function getAuthAdapter()
    {
    	//Zend_Loader::loadFile('Zend/Auth/Adapter/DbTable.php');
        //$dbAdapter = Zend_Registry::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);

        $authAdapter->setTableName('user')
                    ->setIdentityColumn('nickname')
                    ->setCredentialColumn('password')
                    ->setCredentialTreatment('(?)');
                    
        return $authAdapter;
    }

    /**
     * Create and return the login form
     *
     * @return object
     */
    protected function getLoginForm()
    {
    	//Zend_Loader::loadFile('Zend/Form/Element/Text.php');
    	//Zend_Loader::loadFile('Zend/Form/Element/Password.php');
        //Zend_Loader::loadFile('Zend/Form/Element/Submit.php');
        //Zend_Loader::loadFile('Zend/Form.php');
        $nickname = new Zend_Form_Element_Text('nickname');
        $nickname->setLabel('Nickname:')
                ->setRequired(true);

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
                ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('login');
        $submit->setLabel('Login');

        $loginForm = new Zend_Form();
        $loginForm->setAction($this->_request->getBaseUrl().'/login/index/')
                ->setMethod('post')
                ->addElement($nickname)
                ->addElement($password)
                ->addElement($submit);

        return $loginForm;
    }
    public function forbiddenAction(){
    	
    }
  
  public function adminviewAction()
  {
  	 $this->_helper->layout->setLayout('admin');
  	 $userInfo = Zend_Auth::getInstance()->getStorage()->read();
	
	$this->view->nickname = $userInfo->nickname;
	$this->view->name = $userInfo->firstname;
	$this->view->email = $userInfo->email_address;
	$this->view->userlevel = $userInfo->userlevel;
  }
  
}
?>
